package com.ellfors.module_first.index_rcv

import android.content.Intent
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.ellfors.common.base.BaseActivity
import com.ellfors.common.base.EmptyViewModel
import com.ellfors.common.ext.dp2px
import com.ellfors.common.ext.jsonToList
import com.ellfors.common.ext.toast
import com.ellfors.module_first.databinding.ActivityIndexRcvBinding
import com.ellfors.module_first.index_rcv.widget.SuspensionDecoration

/**
 * @date: 2024/06/21
 * @author: ly
 * @Description:带索引并且悬浮title的RecyclerView
 */
class IndexRcvActivity : BaseActivity<ActivityIndexRcvBinding, EmptyViewModel>() {

    private val mCountryData by lazy {
        buildCountry()
    }

    private val mAdapter by lazy {
        IndexRcvAdapter()
    }

    private val mDecoration by lazy {
        SuspensionDecoration(this)
    }

    companion object {
        fun start(activity: AppCompatActivity) {
            Intent(activity, IndexRcvActivity::class.java).apply {
                activity.startActivity(this)
            }
        }
    }

    override fun initView() {
        showTitleBar("测试索引RecyclerView")

        mBinding.recyclerview.apply {
            layoutManager = LinearLayoutManager(this@IndexRcvActivity)
            adapter = mAdapter

            //初始化悬停相关
            mDecoration
                .setTitleHeight(24.dp2px())
                .setTitleBgColor(Color.WHITE)
                .setTitleFontSize(16.dp2px())
                .setTitleFontBold()
                .setTitleFontColor(Color.parseColor("#292A33"))
                .setTitlePaddingLeft(16.dp2px())
            addItemDecoration(mDecoration)
        }
        mAdapter.setOnItemClickListener { adapter, view, position ->
            adapter.getItem(position)?.also {
                it.country.toast()
                mAdapter.setSelectedCountry(it)
            }
        }

        //侧边栏
        mBinding.indexbar.setNeedRealIndex(true).bindRecyclerView(mBinding.recyclerview)
    }

    override fun initData() {
        //测试下默认选中
        val selected: CountryBean? = CountryBean("kg", "Kyrgyzstan")
        selected?.also { mAdapter.setSelectedCountry(it) }

        val sortList = mCountryData.sortedBy {
            //按字母排序，虽然服务端已经做了这个逻辑，但是为了防止出现意外，所以也要处理下
            it.country
        }
        //设置数据
        mAdapter.submitList(sortList)
        mDecoration.setData(sortList)
        mBinding.indexbar.setSourceData(sortList).invalidate()

        selected?.also {
            sortList.forEachIndexed { index, bean ->
                //默认定位到上次选择的Item，给点偏移量让他居中
                if (bean.code == selected.code) {
                    (mBinding.recyclerview.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(
                        index, 500
                    )
                    return@also
                }
            }
        }
    }

    private fun buildCountry(): ArrayList<CountryBean> {
        val jsonStr =
            "[{\"code\":\"np\",\"country\":\"Nepal\"},{\"code\":\"na\",\"country\":\"Namibia\"},{\"code\":\"af\",\"country\":\"Afghanistan\"},{\"code\":\"ax\",\"country\":\"Aland Islands\"},{\"code\":\"al\",\"country\":\"Albania\"},{\"code\":\"dz\",\"country\":\"Algeria\"},{\"code\":\"as\",\"country\":\"American Samoa\"},{\"code\":\"ad\",\"country\":\"Andorra\"},{\"code\":\"ao\",\"country\":\"Angola\"},{\"code\":\"ai\",\"country\":\"Anguilla\"},{\"code\":\"aq\",\"country\":\"Antarctica\"},{\"code\":\"ag\",\"country\":\"Antigua and Barbuda\"},{\"code\":\"ar\",\"country\":\"Argentina\"},{\"code\":\"am\",\"country\":\"Armenia\"},{\"code\":\"aw\",\"country\":\"Aruba\"},{\"code\":\"au\",\"country\":\"Australia\"},{\"code\":\"at\",\"country\":\"Austria\"},{\"code\":\"az\",\"country\":\"Azerbaijan\"},{\"code\":\"bs\",\"country\":\"Bahamas (The)\"},{\"code\":\"bh\",\"country\":\"Bahrain\"},{\"code\":\"bd\",\"country\":\"Bangladesh\"},{\"code\":\"bb\",\"country\":\"Barbados\"},{\"code\":\"by\",\"country\":\"Belarus\"},{\"code\":\"be\",\"country\":\"Belgium\"},{\"code\":\"bz\",\"country\":\"Belize\"},{\"code\":\"bj\",\"country\":\"Benin\"},{\"code\":\"bm\",\"country\":\"Bermuda\"},{\"code\":\"bt\",\"country\":\"Bhutan\"},{\"code\":\"bo\",\"country\":\"Bolivia\"},{\"code\":\"ba\",\"country\":\"Bosnia and Herzegovina\"},{\"code\":\"bw\",\"country\":\"Botswana\"},{\"code\":\"bv\",\"country\":\"Bouvet Island\"},{\"code\":\"br\",\"country\":\"Brazil\"},{\"code\":\"io\",\"country\":\"British Indian Ocean Territory (the)\"},{\"code\":\"bn\",\"country\":\"Brunei Darussalam\"},{\"code\":\"bg\",\"country\":\"Bulgaria\"},{\"code\":\"bf\",\"country\":\"Burkina Faso\"},{\"code\":\"bi\",\"country\":\"Burundi\"},{\"code\":\"kh\",\"country\":\"Cambodia\"},{\"code\":\"cm\",\"country\":\"Cameroon\"},{\"code\":\"ca\",\"country\":\"Canada\"},{\"code\":\"cv\",\"country\":\"Cape Verde\"},{\"code\":\"ky\",\"country\":\"Cayman Islands (the)\"},{\"code\":\"cf\",\"country\":\"Central African Republic (the)\"},{\"code\":\"td\",\"country\":\"Chad\"},{\"code\":\"cl\",\"country\":\"Chile\"},{\"code\":\"cn\",\"country\":\"China\"},{\"code\":\"cx\",\"country\":\"Christmas Island\"},{\"code\":\"cc\",\"country\":\"Cocos (Keeling) Islands (the)\"},{\"code\":\"co\",\"country\":\"Colombia\"},{\"code\":\"km\",\"country\":\"Comoros\"},{\"code\":\"cg\",\"country\":\"Congo\"},{\"code\":\"cd\",\"country\":\"Congo (the Democratic Republic of the)\"},{\"code\":\"ck\",\"country\":\"Cook Islands (the)\"},{\"code\":\"cr\",\"country\":\"Costa Rica\"},{\"code\":\"ci\",\"country\":\"C?te d'Ivoire\"},{\"code\":\"hr\",\"country\":\"Croatia\"},{\"code\":\"cu\",\"country\":\"Cuba\"},{\"code\":\"cy\",\"country\":\"Cyprus\"},{\"code\":\"cz\",\"country\":\"Czech Republic (the)\"},{\"code\":\"dk\",\"country\":\"Denmark\"},{\"code\":\"dj\",\"country\":\"Djibouti\"},{\"code\":\"dm\",\"country\":\"Dominica\"},{\"code\":\"do\",\"country\":\"Dominican Republic (the)\"},{\"code\":\"ec\",\"country\":\"Ecuador\"},{\"code\":\"eg\",\"country\":\"Egypt\"},{\"code\":\"sv\",\"country\":\"El Salvador\"},{\"code\":\"gq\",\"country\":\"Equatorial Guinea\"},{\"code\":\"er\",\"country\":\"Eritrea\"},{\"code\":\"ee\",\"country\":\"Estonia\"},{\"code\":\"et\",\"country\":\"Ethiopia\"},{\"code\":\"fk\",\"country\":\"Falkland Islands (the) [Malvinas]\"},{\"code\":\"fo\",\"country\":\"Faroe Islands (the)\"},{\"code\":\"fj\",\"country\":\"Fiji\"},{\"code\":\"fi\",\"country\":\"Finland\"},{\"code\":\"fr\",\"country\":\"France\"},{\"code\":\"gf\",\"country\":\"French Guiana\"},{\"code\":\"pf\",\"country\":\"French Polynesia\"},{\"code\":\"tf\",\"country\":\"French Southern Territories (the)\"},{\"code\":\"ga\",\"country\":\"Gabon\"},{\"code\":\"gm\",\"country\":\"Gambia (The)\"},{\"code\":\"ge\",\"country\":\"Georgia\"},{\"code\":\"de\",\"country\":\"Germany\"},{\"code\":\"gh\",\"country\":\"Ghana\"},{\"code\":\"gi\",\"country\":\"Gibraltar\"},{\"code\":\"gr\",\"country\":\"Greece\"},{\"code\":\"gl\",\"country\":\"Greenland\"},{\"code\":\"gd\",\"country\":\"Grenada\"},{\"code\":\"gp\",\"country\":\"Guadeloupe\"},{\"code\":\"gu\",\"country\":\"Guam\"},{\"code\":\"gt\",\"country\":\"Guatemala\"},{\"code\":\"gg\",\"country\":\"Guernsey\"},{\"code\":\"gn\",\"country\":\"Guinea\"},{\"code\":\"gw\",\"country\":\"Guinea-Bissau\"},{\"code\":\"gy\",\"country\":\"Guyana\"},{\"code\":\"ht\",\"country\":\"Haiti\"},{\"code\":\"hm\",\"country\":\"Heard Island and McDonald Islands\"},{\"code\":\"va\",\"country\":\"Holy See (the) [Vatican City State]\"},{\"code\":\"hn\",\"country\":\"Honduras\"},{\"code\":\"hk\",\"country\":\"Hong Kong\"},{\"code\":\"hu\",\"country\":\"Hungary\"},{\"code\":\"is\",\"country\":\"Iceland\"},{\"code\":\"in\",\"country\":\"India\"},{\"code\":\"id\",\"country\":\"Indonesia\"},{\"code\":\"ir\",\"country\":\"Iran (the Islamic Republic of)\"},{\"code\":\"iq\",\"country\":\"Iraq\"},{\"code\":\"ie\",\"country\":\"Ireland\"},{\"code\":\"im\",\"country\":\"Isle of Man\"},{\"code\":\"il\",\"country\":\"Israel\"},{\"code\":\"it\",\"country\":\"Italy\"},{\"code\":\"jm\",\"country\":\"Jamaica\"},{\"code\":\"jp\",\"country\":\"Japan\"},{\"code\":\"je\",\"country\":\"Jersey\"},{\"code\":\"jo\",\"country\":\"Jordan\"},{\"code\":\"kz\",\"country\":\"Kazakhstan\"},{\"code\":\"ke\",\"country\":\"Kenya\"},{\"code\":\"ki\",\"country\":\"Kiribati\"},{\"code\":\"kp\",\"country\":\"Korea (the Democratic People's Republic of)\"},{\"code\":\"kr\",\"country\":\"Korea (the Republic of)\"},{\"code\":\"kw\",\"country\":\"Kuwait\"},{\"code\":\"kg\",\"country\":\"Kyrgyzstan\"},{\"code\":\"la\",\"country\":\"Lao People's Democratic Republic (the)\"},{\"code\":\"lv\",\"country\":\"Latvia\"},{\"code\":\"lb\",\"country\":\"Lebanon\"},{\"code\":\"ls\",\"country\":\"Lesotho\"},{\"code\":\"lr\",\"country\":\"Liberia\"},{\"code\":\"ly\",\"country\":\"Libyan Arab Jamahiriya (the)\"},{\"code\":\"li\",\"country\":\"Liechtenstein\"},{\"code\":\"lt\",\"country\":\"Lithuania\"},{\"code\":\"lu\",\"country\":\"Luxembourg\"},{\"code\":\"mo\",\"country\":\"Macao\"},{\"code\":\"mk\",\"country\":\"Macedonia (the former Yugoslav Republic of)\"},{\"code\":\"mg\",\"country\":\"Madagascar\"},{\"code\":\"mw\",\"country\":\"Malawi\"},{\"code\":\"my\",\"country\":\"Malaysia\"},{\"code\":\"mv\",\"country\":\"Maldives\"},{\"code\":\"ml\",\"country\":\"Mali\"},{\"code\":\"mt\",\"country\":\"Malta\"},{\"code\":\"mh\",\"country\":\"Marshall Islands (the)\"},{\"code\":\"mq\",\"country\":\"Martinique\"},{\"code\":\"mr\",\"country\":\"Mauritania\"},{\"code\":\"mu\",\"country\":\"Mauritius\"},{\"code\":\"yt\",\"country\":\"Mayotte\"},{\"code\":\"mx\",\"country\":\"Mexico\"},{\"code\":\"fm\",\"country\":\"Micronesia (the Federated States of)\"},{\"code\":\"md\",\"country\":\"Moldova (the Republic of)\"},{\"code\":\"mc\",\"country\":\"Monaco\"},{\"code\":\"mn\",\"country\":\"Mongolia\"},{\"code\":\"me\",\"country\":\"Montenegro\"},{\"code\":\"ms\",\"country\":\"Montserrat\"},{\"code\":\"ma\",\"country\":\"Morocco\"},{\"code\":\"mz\",\"country\":\"Mozambique\"},{\"code\":\"mm\",\"country\":\"Myanmar\"},{\"code\":\"nr\",\"country\":\"Nauru\"},{\"code\":\"nl\",\"country\":\"Netherlands (the)\"},{\"code\":\"an\",\"country\":\"Netherlands Antilles (the)\"},{\"code\":\"nc\",\"country\":\"New Caledonia\"},{\"code\":\"nz\",\"country\":\"New Zealand\"},{\"code\":\"ni\",\"country\":\"Nicaragua\"},{\"code\":\"ne\",\"country\":\"Niger (the)\"},{\"code\":\"ng\",\"country\":\"Nigeria\"},{\"code\":\"nu\",\"country\":\"Niue\"},{\"code\":\"nf\",\"country\":\"Norfolk Island\"},{\"code\":\"mp\",\"country\":\"Northern Mariana Islands (the)\"},{\"code\":\"no\",\"country\":\"Norway\"},{\"code\":\"om\",\"country\":\"Oman\"},{\"code\":\"pk\",\"country\":\"Pakistan\"},{\"code\":\"pw\",\"country\":\"Palau\"},{\"code\":\"ps\",\"country\":\"Palestinian Territory (the Occupied)\"},{\"code\":\"pa\",\"country\":\"Panama\"},{\"code\":\"pg\",\"country\":\"Papua New Guinea\"},{\"code\":\"py\",\"country\":\"Paraguay\"},{\"code\":\"pe\",\"country\":\"Peru\"},{\"code\":\"ph\",\"country\":\"Philippines (the)\"},{\"code\":\"pn\",\"country\":\"Pitcairn\"},{\"code\":\"pl\",\"country\":\"Poland\"},{\"code\":\"pt\",\"country\":\"Portugal\"},{\"code\":\"pr\",\"country\":\"Puerto Rico\"},{\"code\":\"qa\",\"country\":\"Qatar\"},{\"code\":\"re\",\"country\":\"R��union\"},{\"code\":\"ro\",\"country\":\"Romania\"},{\"code\":\"ru\",\"country\":\"Russian Federation (the)\"},{\"code\":\"rw\",\"country\":\"Rwanda\"},{\"code\":\"sh\",\"country\":\"Saint Helena\"},{\"code\":\"kn\",\"country\":\"Saint Kitts and Nevis\"},{\"code\":\"lc\",\"country\":\"Saint Lucia\"},{\"code\":\"pm\",\"country\":\"Saint Pierre and Miquelon\"},{\"code\":\"vc\",\"country\":\"Saint Vincent and the Grenadines\"},{\"code\":\"ws\",\"country\":\"Samoa\"},{\"code\":\"sm\",\"country\":\"San Marino\"},{\"code\":\"st\",\"country\":\"Sao Tome and Principe\"},{\"code\":\"sa\",\"country\":\"Saudi Arabia\"},{\"code\":\"sn\",\"country\":\"Senegal\"},{\"code\":\"rs\",\"country\":\"Serbia\"},{\"code\":\"sc\",\"country\":\"Seychelles\"},{\"code\":\"sl\",\"country\":\"Sierra Leone\"},{\"code\":\"sg\",\"country\":\"Singapore\"},{\"code\":\"sk\",\"country\":\"Slovakia\"},{\"code\":\"si\",\"country\":\"Slovenia\"},{\"code\":\"sb\",\"country\":\"Solomon Islands (the)\"},{\"code\":\"so\",\"country\":\"Somalia\"},{\"code\":\"za\",\"country\":\"South Africa\"},{\"code\":\"gs\",\"country\":\"South Georgia and the South Sandwich Islands\"},{\"code\":\"es\",\"country\":\"Spain\"},{\"code\":\"lk\",\"country\":\"Sri Lanka\"},{\"code\":\"sd\",\"country\":\"Sudan (the)\"},{\"code\":\"sr\",\"country\":\"Suriname\"},{\"code\":\"sj\",\"country\":\"Svalbard and Jan Mayen\"},{\"code\":\"sz\",\"country\":\"Swaziland\"},{\"code\":\"se\",\"country\":\"Sweden\"},{\"code\":\"ch\",\"country\":\"Switzerland\"},{\"code\":\"sy\",\"country\":\"Syrian Arab Republic (the)\"},{\"code\":\"tw\",\"country\":\"Taiwan (Province of China)\"},{\"code\":\"tj\",\"country\":\"Tajikistan\"},{\"code\":\"tz\",\"country\":\"Tanzania United Republic of\"},{\"code\":\"th\",\"country\":\"Thailand\"},{\"code\":\"tl\",\"country\":\"Timor-Leste\"},{\"code\":\"tg\",\"country\":\"Togo\"},{\"code\":\"tk\",\"country\":\"Tokelau\"},{\"code\":\"to\",\"country\":\"Tonga\"},{\"code\":\"tt\",\"country\":\"Trinidad and Tobago\"},{\"code\":\"tn\",\"country\":\"Tunisia\"},{\"code\":\"tr\",\"country\":\"Turkey\"},{\"code\":\"tm\",\"country\":\"Turkmenistan\"},{\"code\":\"tc\",\"country\":\"Turks and Caicos Islands (the)\"},{\"code\":\"tv\",\"country\":\"Tuvalu\"},{\"code\":\"ug\",\"country\":\"Uganda\"},{\"code\":\"ua\",\"country\":\"Ukraine\"},{\"code\":\"ae\",\"country\":\"United Arab Emirates (the)\"},{\"code\":\"gb\",\"country\":\"United Kingdom (the)\"},{\"code\":\"us\",\"country\":\"United States (the)\"},{\"code\":\"um\",\"country\":\"United States Minor Outlying Islands (the)\"},{\"code\":\"uy\",\"country\":\"Uruguay\"},{\"code\":\"uz\",\"country\":\"Uzbekistan\"},{\"code\":\"vu\",\"country\":\"Vanuatu\"},{\"code\":\"ve\",\"country\":\"Venezuela\"},{\"code\":\"vn\",\"country\":\"Viet Nam\"},{\"code\":\"vg\",\"country\":\"Virgin Islands (British)\"},{\"code\":\"vi\",\"country\":\"Virgin Islands (U.S.)\"},{\"code\":\"wf\",\"country\":\"Wallis and Futuna\"},{\"code\":\"eh\",\"country\":\"Western Sahara\"},{\"code\":\"ye\",\"country\":\"Yemen\"},{\"code\":\"zm\",\"country\":\"Zambia\"},{\"code\":\"zw\",\"country\":\"Zimbabwe\"}]"
        return jsonStr.jsonToList<CountryBean>()
    }
}